/*
 * esp8266.c
 *
 *  Created on: Aug 7, 2021
 *      Author: 1253008684
 */
#include <stdlib.h>
#include "usart.h"
#include "esp8266.h"

int send_atcmd(char *atcmd, unsigned int timeout)
{
	int                       rv = -1;
	unsigned int              i;//0-4294967295


	if(  !atcmd || strlen(atcmd)<=0 )//check arguments validation
	{
		esp_printf("ERROR %s:%d %s(): Invalid input arguments\r\n", __FILE__, __LINE__, __FUNCTION__);
		return -1;
	}

	esp_printf("\r\nStart send AT command: %s\r\n", atcmd);

	clear_uart2_buf();//memset(g_atcmd_rxbuf, 0, sizeof(g_atcmd_rxbuf))

	HAL_UART_Transmit(esp8266_huart, (uint8_t *)atcmd, strlen(atcmd), 1000);
	//当我们发送一个数据到esp8266WiFi模块，它会reply;

	for(i=0; i<timeout; i++)
	{
		if( strstr(g_atcmd_rxbuf, "OK\r\n") )
		{
			rv = 0;
			goto CleanUp;
		}

		if( strstr(g_atcmd_rxbuf, "ERROR\r\n") )
		{
			rv = 1;
			goto CleanUp;
		}
		if (strstr(g_atcmd_rxbuf, "FAIL\r\n"))//connect wifi error send FAIL\r\n;
		{
			rv = 2;
			goto CleanUp;

		}

		HAL_Delay(50);
	}


CleanUp:
	esp_printf("<<<< AT command reply:\r\n%s (%d)", g_atcmd_rxbuf, rv);
	return rv;
}

int atcmd_send_data(unsigned char *data, int bytes, unsigned int timeout)
{
	int                       rv = -1;
	unsigned int              i;


	if(  !data || bytes <= 0 )
	{
		esp_printf("ERROR %s:%d %s(): Invalid input arguments\r\n", __FILE__, __LINE__, __FUNCTION__);
		return -1;
	}

	esp_printf("\r\nStart AT command send [%d] bytes data\r\n", bytes);

	clear_uart2_buf();

	HAL_UART_Transmit(esp8266_huart, data, bytes, 1000);
	////当我们发送一个数据到esp8266WiFi模块，它会reply;

	for(i=0; i<timeout; i++)
	{
		if( strstr(g_atcmd_rxbuf, "OK\r\n") )
		{
			rv = 0;
			goto CleanUp;
		}

		if( strstr(g_atcmd_rxbuf, "ERROR\r\n") )
		{
			rv = 1;
			goto CleanUp;
		}

		HAL_Delay(50);
	}


CleanUp:
	esp_printf("<<<< AT command reply:\r\n%s", g_atcmd_rxbuf);
	return rv;
}



int esp8266_wifi_init(void)
{
	int 			rv = -1;
	rv = send_atcmd("AT+RST", 500);
	if (0 != rv )
	{
		rv = 1;
		goto CleanUp;
	}

	HAL_Delay(20);
	clear_uart2_buf();

	rv = send_atcmd("AT", 500);
	if (0 != rv )
	{
		rv = 2;
		goto CleanUp;
	}
	clear_uart2_buf();

	rv = send_atcmd("AT+GMR", 500);
	if (0 != rv )
	{
		rv = 3;
		goto CleanUp;
	}
	clear_uart2_buf();

	rv = send_atcmd("AT+CWMODE_CUR=1", 500);
	if (0 != rv )
	{
		rv = 4;
		goto CleanUp;

	}

	rv = send_atcmd("AT+CWDHCP_CUR=1,1", 500);
	if (0 != rv )
	{
		rv = 5;
		goto CleanUp;
	}

CleanUp:
		esp_printf("AT command config error!:\r\n%s,%d", g_atcmd_rxbuf, rv);
		return rv;

}

int esp8266wifi_join_network(char *id, char *password)
{
	char           atcmd[128];
	int 			rv = -1;

	if( !id || !password )
	{
		return -1;
	}

	memset(atcmd, 0, sizeof(atcmd));//这里的清空并不与send_atcmd（）的clear_uart2_buf()冲突；
	snprintf(atcmd, sizeof(atcmd), "AT+CWJAP_CUR=\"%s\",\"%s\"\r\n", id, password);

	rv = send_atcmd(atcmd, 8000);
	if (rv != 0)
	{
		rv = 1;
		esp_printf("AT command config(wifi-sock-network) error!:\r\n%s,%d", g_atcmd_rxbuf, rv);
	}
	rv = send_atcmd("AT+CIPSTA_CUR?\r\n", 1000);
	if (rv != 0)
	{
		rv = 2;
		esp_printf("AT command config(check get ip or not) error!:\r\n%s,%d", g_atcmd_rxbuf, rv);

	}
	return rv;
}

int esp8266wifi_ping(char *id)
{
	int 		rv = -1;
	char 		atcmd[32];

	if( !id )
	{
		return -1;
	}

	memset(atcmd, 0, sizeof(atcmd));
	snprintf(atcmd, sizeof(atcmd), "AT+PING=\"%s\"\r\n", id);

	rv = send_atcmd(atcmd, 2000);
	if (rv != 0)
	{
		rv = 1;
		esp_printf("AT command config(ping ip error,can not connect network!) error!:\r\n%s,%d", g_atcmd_rxbuf, rv);
	}

	return rv;
}


int esp8266wifi_socket_connect(char *servip, int port)
{
	char           atcmd[128];
	int            rv = -1;

	if( !servip || port<=0 )
		return -1;

	rv = send_atcmd("AT+CIPMUX=0\r\n", 1500);
	if (rv != 0)
	{
		rv = 1;
		esp_printf ("AT command config(ban else network)error!:\r\n%s,%d", g_atcmd_rxbuf, rv);
	}

	memset(atcmd, 0, sizeof(atcmd));
	snprintf(atcmd, sizeof(atcmd), "AT+CIPSTART=\"TCP\",\"%s\",%d\r\n", servip, port);

	rv = send_atcmd(atcmd, 1000);
	if (rv != 0)
	{
		rv = 2;
		esp_printf ("AT command config(esp8266wifi connect socket error) error!:\r\n%s,%d", g_atcmd_rxbuf, rv);
	}
	return 0;
}

int esp8266_sock_disconnect(void)
{
	int 		rv = -1;
	rv = send_atcmd("AT+CIPCLOSE\r\n", 1500);
	if (rv != 0)
	{
		rv = 1;
		esp_printf ("AT command config(esp8266_socket_disconnceted) error!:\r\n%s,%d", g_atcmd_rxbuf, rv);
	}
	return 0;
}

int esp8266_sock_send(unsigned char *data, int bytes)
{
	int 		rv = -1;
	char        atcmd[64];

	memset(atcmd, 0, sizeof(atcmd));
	snprintf(atcmd, sizeof(atcmd), "AT+CIPSEND=%d\r\n", bytes);

	rv = send_atcmd(atcmd, 500);
	if (rv != 0)
	{
		rv = 1;
		esp_printf ("AT command config(esp8266_socket_send) error!:\r\n%s,%d", g_atcmd_rxbuf, rv);
	}

	rv = atcmd_send_data(data, bytes, 1000);
	if (rv != 0)
	{
		rv = 2;
		esp_printf ("AT command config(esp8266_socket_send) error!:\r\n%s,%d", g_atcmd_rxbuf, rv);
	}

	return bytes;
}


int esp8266_sock_recv(unsigned char *buf, int size)
{
	char                *data = NULL;
	char                *ptr = NULL;

	int                  len;
	int                  rv;
	int                  bytes;

	if( g_atcmd_bytes<= 0 )
	{
		return 0;
	}

	/* No data arrive or not integrated */
	if( !(ptr=strstr(g_atcmd_rxbuf, "+IPD,")) ||  !(data=strchr( g_atcmd_rxbuf, ':' )) )
	{
		return 0;
	}

	data ++;
	bytes = atoi(ptr+strlen("+IPD,"));

	len = g_atcmd_bytes - (data-g_uart2_rxbuf);

	if( len < bytes )
	{
		esp_printf("+IPD data not receive over, receive again later ...\r\n");
		return 0;
	}

	rv = bytes>size ? size : bytes;
	memcpy(buf, data, rv);

	clear_uart2_buf();

	return rv;
}






